/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2023-12-20     80000022       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>

#define EEPROM_DEVICE_ADDRESS 0x40
#define I2C_BUS_NAME "i2c1"

static struct rt_i2c_bus_device *i2c_bus;

void enable_write_protection(void)
{
    rt_size_t ret;
    uint8_t write_data[2];
    write_data[0] = 0x00; // ���ƼĴ�����ַ
    write_data[1] = 0x80; // ����д�������ο�оƬ�������ֲ�������ȷ��ֵ
  
    ret = rt_i2c_master_send(i2c_bus, EEPROM_DEVICE_ADDRESS, 0, write_data, sizeof(write_data));
    if (ret < 0)
    {
        rt_kprintf("%s: rt_i2c_master_send err, ret = %d\n", __func__, ret);
    }
}

void read_from_eeprom(uint16_t address, uint8_t* data, size_t length)
{
    rt_size_t ret;
    uint8_t write_data[2];
    write_data[0] = (address >> 8); // д���ַ�ĸ�λ�ֽ�
    write_data[1] = (address & 0xFF); // д���ַ�ĵ�λ�ֽ�
  
    ret = rt_i2c_master_send(i2c_bus, EEPROM_DEVICE_ADDRESS, 0,write_data, sizeof(write_data));
    if (ret < 0)
    {
        rt_kprintf("%s: rt_i2c_master_send err, ret = %d\n", __func__, ret);
    }
  
    ret = rt_i2c_master_recv(i2c_bus, EEPROM_DEVICE_ADDRESS, 0,data, length);
    if (ret < 0)
    {
        rt_kprintf("%s: rt_i2c_master_recv err, ret = %d\n", __func__, ret);
    }
}

void write_to_eeprom(uint16_t address, const uint8_t* data, size_t length)
{
    rt_size_t ret;
    uint8_t write_data[length + 2];
    write_data[0] = (address >> 8); // д���ַ�ĸ�λ�ֽ�
    write_data[1] = (address & 0xFF); // д���ַ�ĵ�λ�ֽ�
  
    rt_memcpy(&write_data[2], data, length);
  
    ret = rt_i2c_master_send(i2c_bus, EEPROM_DEVICE_ADDRESS, 0, write_data, sizeof(write_data));
    if (ret < 0)
    {
        rt_kprintf("%s: rt_i2c_master_send err, ret = %d\n", __func__, ret);
    }
}

static void eeprom_test(void)
{
    uint8_t read_data[20] = {0};
    uint8_t *str = "hkx";
    RT_ASSERT(i2c_bus != RT_NULL);
    enable_write_protection();
    write_to_eeprom(EEPROM_DEVICE_ADDRESS, str, 4);
    read_from_eeprom(EEPROM_DEVICE_ADDRESS, read_data, sizeof(read_data));
    rt_kprintf("%s: read_data: %s\n", __func__, read_data);
}

MSH_CMD_EXPORT(eeprom_test, eeprom test);

static void iic_test2d(void)
{
    rt_size_t ret;
    uint8_t data = 0x2d;
    RT_ASSERT(i2c_bus != RT_NULL); 
    ret = rt_i2c_master_send(i2c_bus, EEPROM_DEVICE_ADDRESS, 0, &data, sizeof(data));
    if (ret < 0)
    {
        rt_kprintf("%s: rt_i2c_master_send err, ret = %d\n", __func__, ret);
    }
}

MSH_CMD_EXPORT(iic_test2d, iic test2d);

static void iic_test15(void)
{
    rt_size_t ret;
    uint8_t data = 0x15;
    RT_ASSERT(i2c_bus != RT_NULL);
    ret = rt_i2c_master_send(i2c_bus, EEPROM_DEVICE_ADDRESS, 0, &data, sizeof(data));
    if (ret < 0)
    {
        rt_kprintf("%s: rt_i2c_master_send err, ret = %d\n", __func__, ret);
    }
}

MSH_CMD_EXPORT(iic_test15, iic test15);

int eeprom_example_init(void)
{
    i2c_bus = rt_i2c_bus_device_find(I2C_BUS_NAME);
    if (i2c_bus == RT_NULL)
    {
        rt_kprintf("%s: i2c bus %s not found!\n", __func__, I2C_BUS_NAME);
        return -RT_ERROR;
    }
    rt_kprintf("%s: %s found success!\n", __func__, I2C_BUS_NAME);
    return RT_EOK;
}

INIT_APP_EXPORT(eeprom_example_init);
